﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CommonUtil
{

    public class DiscryptPWD
    {
        string[] MODS = new string[7] { "", "ke3wms", "jnx9i8", "dpqzl6", "vyg1bh", "7a4c2f", "u5rto0" };
        string[] SDOM = new string[7] { "", "wa6j1c", "fblrk7", "mg9q0s", "vu3zth", "po4xy2", "ni5d8e" };
        string[] DOMS = new string[7] { "", "mg9qwc", "vu3zth", "a6j10s", "fbk7lr", "5dpoy2", "4xni8e" };

        public DiscryptPWD()
        { }

        public string discryptpassword(string apassword)
        {
            string result = "";
            int tmpi, i;
            result = lattertosixstr(apassword, SDOM);
            if (result.Length < 3)
            {
                result = "";
                return result;
            }
            tmpi = 0;
            for (i = 0; i < 3; i++)
            {
                tmpi = tmpi * 6 + Convert.ToInt32(result.Substring(i, 1));
            }
            result = result.Substring(3, tmpi);
            result = sixstrtolatter(result, MODS);
            return result;
        }

        private string lattertosixstr(string astr, string[] SDOM)
        {
            int i = 0, n = 0, p = 0;
            string tmps1, tmps2;
            string result = string.Empty;
            tmps1 = "";
            tmps2 = "";

            for (i = 0; i < astr.Length; i++)
            {
                for (n = 1; n <= 6; n++)
                {
                    string aaa = SDOM[n];
                    string tmpaStr = astr.Substring(i, 1);
                    p = aaa.IndexOf(tmpaStr);
                    p = p + 1;
                    if (p > 0)
                    {
                        tmps1 = tmps1 + (n - 1).ToString();
                        tmps2 = (p - 1).ToString() + tmps2;
                        break;
                    }
                }
            }
            result = tmps1 + tmps2;
            return result;
        }

        private string sixstrtolatter(string astr, string[] MODS)
        {
            string result = "";
            int i, n;
            n = astr.Length;

            if ((n % 2) == 1)
            {
                return "";
            }
            int len = n / 2;
            string str1 = astr.Substring(0, len);
            string str2 = ReverseE(astr.Substring(len, len));

            for (i = 0; i < (n / 2); i++)
            {
                int b = (int)'0';
                int a1 = 0;
                int a2 = 0;
                char c1 = Convert.ToChar(str1.Substring(i, 1));
                char c2 = Convert.ToChar(str2.Substring(i, 1));
                a1 = (int)c1 - b + 1;
                a2 = (int)c2 - b;

                string modstr = (string.IsNullOrEmpty(MODS[a1])) ? "" : MODS[a1].Substring(a2, 1);
                result = result + modstr;
            }
            return result;
        }

        private string ReverseE(string text)
        {
            char[] cArray = text.ToCharArray();
            StringBuilder reverse = new StringBuilder();
            for (int i = cArray.Length - 1; i > -1; i--)
            {
                reverse.Append(cArray[i]);
            }
            return reverse.ToString();
        }
    }
}
